module.exports = {
  data: {
    someData: 'myMixin',
    xcsys: {},
    pagetitle: "",
    shareimg: "",
    conttop: 0,
    sharefoot: true,
    eg: {},
    loadconfigstatus: -1
  },
  onShow() {},
  onLoad(options) {
    var that=this;
    if(wx.onNeedPrivacyAuthorization){
      wx.onNeedPrivacyAuthorization((resolve, eventInfo) => {
        console.log('触发本次事件的接口是：' + eventInfo.referrer)
        // 需要用户同意隐私授权时
        // 弹出开发者自定义的隐私授权弹窗
        that.selectComponent("#privacypopup").show()
       
        this.resolvePrivacyAuthorization = resolve
      })
    }

    var app = getApp()
    if (options.scene) {
      options = this.changescene(options)
    }
    
    if (options.shareid) {
      app.globalData.share.shareid = options.shareid
    }
    this.data.options = options;
  
    var that = this;
    this.setData({
      integralname:app.globalData.integralname,
      CustomBar: app.globalData.CustomBar
    })
    app.xc_sys_config.loadconfig(that).then(reslut => {
      var res = reslut["configdata"]
     
      if(res['integral'] && res['integral']['customtext'] && res['integral']['customtext']['integral']){
        app.globalData.integralname=res['integral']['customtext']['integral']
        that.setData({
          integralname:res['integral']['customtext']['integral'],
        })

      }
      if (res['basic']) {
        var update = {}
        if (res['basic']['simg']) {
          update['shareimg'] = res['basic']['simg']
        }
        if (res['basic']['sharename']) {
          update['sharetitle'] = res['basic']['sharename']
        }
        if (res['basic']['sharepage']) {
          update['sharepage'] = res['basic']['sharepage']
        }

        this.setData(update)
      }

      var themcolor = null
      if (res.prothem) {
        try {
          themcolor = res.prothem.themcolor.config
        } catch (ex) {

        }
      }

      that.setData({
        eg: res.eg,
        xcsys: reslut["xcsys"],
        loadconfigstatus: 1,
        themcolor
      })
      if (that.data.sharefoot) {
        app.xc_sys_config.setpagelayout(that, app);
      }

      if (typeof (that.xcloaddata) == "function") {
        that.xcloaddata(options, res)
      }
      reslut["userfunc"].then(useitem => {
        console.log("userfunc")
        console.log(res)

        if (typeof (that.afterlogin) == "function") {
          that.afterlogin(options, res, useitem["status"] == 1 ? useitem.obj : null)
        }
        if(useitem["status"] == 1){
            var obj=useitem.obj;
            if(!obj['uid']){
              if(res['authorization'] && res['authorization']['status']==1){
                app.xc_sys_config.authop.oppower("login").then(function (res) {
                  that.setData({
                    "member": app.globalData.member
                  })
                })
              }
            }
            
        }
        
      })

      //  app.xc_sys_config.setpagelayout(that,app)
    })
    if (this.data.ishare) {
      wx.showShareMenu({
        withShareTicket: true,
        menus: ['shareAppMessage', 'shareTimeline']
      })
    }



    if (!app.globalData.basicurl || app.globalData.basicurl == -1) {

      this.setbasicurl()
    } else {
      this.setData({
        basicurl: app.globalData.basicurl
      })
    }
  },
  agree() {
    // 用户点击同意按钮后
    this.resolvePrivacyAuthorization({ buttonId: 'agree-btn', event: 'agree' })
    // 用户点击同意后，开发者调用 resolve({ buttonId: 'agree-btn', event: 'agree' })  告知平台用户已经同意，参数传同意按钮的id
    // 用户点击拒绝后，开发者调用 resolve({ event:'disagree' }) 告知平台用户已经拒绝
  },
  disagree(){
  
    this.resolvePrivacyAuthorization({event:'disagree' })

  },
  changescene(options) {
    var scene = decodeURIComponent(options.scene); //lib-sys-wxapphelper
    var mapping = {
      '1x': 'id',
      '2x': 'activityid',
      '3x': 'groupid',
      '4x': 'clubid',
      '5x': 'diyid',
      '6x': 'couponid',
      '7x': 'uid',
      '8x': 'taskid',
      '9x': 'depositid',
      '10x': 'price'
    }
    var scenedata = scene.split("_");
  
    options['shareid'] = scenedata[0];
    var len = scenedata.length;

    if (len >= 2) {
      for (var i = 1; i < len; i++) {
        var arr=scenedata[i].split("x")
        var key = arr[0]+'x';
        console.log(key)
        if (mapping.hasOwnProperty(key)) {
          options[mapping[key]] = arr[1];
        }
      }
    }
    return options
  },
  navigateTo(ev) {
    var dataset = ev.currentTarget.dataset
    var url = dataset.url;
    var app = getApp()

    if (dataset.auth && dataset.auth == 1) {
      app.xc_sys_config.authop.oppower("login").then(function (res) {
        app.xc_sys_config.router.navigateTo(url)
      })

    } else {
      app.xc_sys_config.router.navigateTo(url)
    }

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

    var app = getApp()
    var sharetitle = this.shasretitle()

    var shareimg = this.data.shareimg


    var sharepath = ""
    if (this.data.sharepage) {


      sharepath = this.data.sharepage
      if (app.globalData.member && app.globalData.member.member_id) {
        sharepath = sharepath + "?shareid=" + app.globalData.member.member_id
      }
    } else {
      sharepath = app.xc_sys_config.getshareurl()
    }
   console.log("shareimg")
   console.log(sharetitle)
   console.log(sharepath)
   console.log(shareimg)

    return {
      title: sharetitle,
      path: sharepath,
      imageUrl:shareimg
    }
  },
  onShareTimeline() {
    var app = getApp()
    var sharetitle = this.shasretitle()
    const sharepara = app.xc_sys_config.getshareurl("para")
    return {
      title: sharetitle,
      query: sharepara,
      imageUrl: this.data.shareimg

    }
  },
  shasretitle() {
    var app = getApp()
    if (this.data.sharetitle) {
      return this.data.sharetitle
    } else if (app.globalData.configdata) {
      return app.globalData.configdata.basic.name
    }
    return ""
  },



  loadstatus(bvalue) {
    if (!bvalue) {
      bvalue = false
    }
    this.setData({
      "loading": bvalue
    })
  },
  authop(authtype) {
    var app = getApp();
    if (!authtype) {
      authtype = "login"
    }
    return new Promise(resolve => {
      app.xc_sys_config.authop.oppower(authtype).then(function (res) {
        if (res["status"] == 1) {
          res["userinfo"] = app.globalData.member
        }
        resolve(res)
      })

    })
  },

  //拨打电话
  xcphonecall(ev) {
    var dataset = ev.currentTarget.dataset

    wx.makePhoneCall({
      phoneNumber: dataset.val,
    })
  },
  //公用复制
  xcopy(ev) {

    var dataset = ev.currentTarget.dataset
    var message = "复制成功"
    if (dataset.message) {
      message = dataset.message
    }
    wx.setClipboardData({
      data: dataset.val,
      success(res) {
        wx.showToast({
          title: message,
        })

      }
    })

  },
  opload(ishow, tiptext) {
    var selectcomponent = this.selectComponent("#opwebtool");
    if (ishow) {
      selectcomponent.showmodel(tiptext)
    } else {
      selectcomponent.closemodel(tiptext)
    } //opwebtool
  },
  login() {
    var app = getApp();


    return;


  },
  setbasicurl() {
    var that = this;
    var app = getApp()

    var basicurl = app.globalData["xcsys"]['basicurl'] + '/addons/' + app.globalData["xcsys"]['m'] + '/';
    that.setData({
      basicurl: basicurl
    })

    app.globalData.basicurl = basicurl;

  },
  //数组去重
  unique(arr) {
    return arr.reduce((prev, cur) => prev.includes(cur) ? prev : [...prev, cur], []);
  },
  showmessage(text, icon) {
    wx.showToast({
      icon: icon,
      title: text,
    })
  },
  error(message, back) {
    const app = getApp()
    var that = this;
    var homeurl = app.globalData.configdata.basic.homeurl
    wx.showModal({
      title: "错误",
      content: message,
      showCancel: false,
      success(res) {
        if (res.confirm) {
          if (back) {
            that.pageback(homeurl)
          }
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  pageback(homeurl, isnav = false) {
    if (!homeurl) {
      const app = getApp()

      if (app.globalData.configdata) {
        homeurl = app.globalData.configdata.basic.homeurl;
      } else {
        homeurl = '/xc_lshop/index/index';
      }
    }
    var pages = getCurrentPages();

    if (isnav) {
      wx.reLaunch({
        url: homeurl,
      })
    } else {
      if (pages[pages.length - 2]) {
        wx.navigateBack({
          delta: 1
        })
      } else {
        wx.reLaunch({
          url: homeurl,
        })
      }
    }

  }
}